﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.CMS.TemplateLanguage.Design;
using MethodWorx.CMS.TemplateLanguage.Properties;

namespace MethodWorx.CMS.TemplateLanguage.Parsers
{
    public class DateFormat : IParser, IDesignable
    {
        [Property]
        public string Format
        {
            get; set;
        }

        public object Parse(Web.CmsRequestContext context, IEnumerable<object> stack, Dictionary<string, object> parameters)
        {
            object s = stack.FirstOrDefault();
            if (!(s is DateTime))
                throw new Exception("Value to parse is not a datetime");
            else
            {
                return ((DateTime) s).ToString(this.Format ?? "G");
            }
        }

       
        public string Name
        {
            get { return "dateformat"; }
        }

        public string Namespace
        {
            get { return "string"; }
        }

        public string[] Parameters
        {
            get { return new string[] { }; }
        }

        public void Design(IDesigner designer)
        {
            designer.AddControl("String:DateFormat")
                .SetControlType(DesignerControlType.Label)
                .AddContentBeforeChildren("(Format: " + DateTime.Now.ToString(this.Format ?? "G") + ")");
        }
    }
}
